
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音频上传与识别</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .fade-in {
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .pulse-upload {
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7); }
            70% { box-shadow: 0 0 0 10px rgba(59, 130, 246, 0); }
            100% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0); }
        }

        .json-container {
            background-color: #1a202c;
            border-radius: 0.5rem;
            padding: 1rem;
            font-family: 'Courier New', monospace;
            white-space: pre-wrap;
            word-break: break-all;
            max-height: 400px;
            overflow-y: auto;
        }

        .file-info {
            background-color: #f7fafc;
            border-radius: 0.5rem;
            padding: 0.75rem;
        }

        .upload-success {
            background-color: rgba(34, 197, 94, 0.1);
            border: 1px solid rgba(34, 197, 94, 0.3);
        }

        .upload-error {
            background-color: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.3);
        }

        .loading-spinner {
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-100 min-h-screen">
<div class="container mx-auto px-4 py-8 max-w-4xl">
    <header class="text-center mb-12 fade-in">
        <h1 class="text-4xl font-bold text-gray-800 mb-2">音频上传与识别</h1>
        <p class="text-gray-600">上传MP3、WebM或WAV音频文件，获取识别结果</p>
    </header>

    <main class="bg-white rounded-xl shadow-lg overflow-hidden fade-in">
        <!-- 上传区域 -->
        <section class="p-8 border-b border-gray-200">
            <h2 class="text-2xl font-semibold text-gray-700 mb-6">上传音频文件</h2>

            <div class="flex flex-col items-center justify-center p-8 border-2 border-dashed border-blue-300 rounded-lg bg-blue-50 pulse-upload transition-all duration-300">
                <i class="fas fa-file-audio text-5xl text-blue-500 mb-4"></i>
                <p class="text-gray-600 mb-4">支持格式: MP3, WebM, WAV</p>

                <form id="uploadForm" class="w-full max-w-md">
                    <div class="mb-4">
                        <label for="audioFile" class="block text-gray-700 text-sm font-medium mb-2">选择音频文件</label>
                        <input type="file" id="audioFile" name="audioFile" accept="audio/mp3,audio/webm,audio/wav,audio/mpeg,audio/x-wav" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div id="fileInfo" class="file-info mb-4 hidden">
                        <p class="text-sm text-gray-700"><span class="font-medium">文件名:</span> <span id="fileName">-</span></p>
                        <p class="text-sm text-gray-700"><span class="font-medium">文件类型:</span> <span id="fileType">-</span></p>
                        <p class="text-sm text-gray-700"><span class="font-medium">文件大小:</span> <span id="fileSize">-</span></p>
                    </div>

                    <button type="button" id="uploadBtn" class="w-full bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors duration-300 flex items-center justify-center">
                        <i class="fas fa-upload mr-2"></i>
                        上传并识别
                    </button>
                </form>

                <div id="uploadStatus" class="w-full max-w-md mt-4 hidden">
                    <div id="statusMessage" class="text-center py-2 px-4 rounded-md"></div>
                </div>
        </section>

        <!-- 识别结果区域 -->
        <section class="p-8">
            <h2 class="text-2xl font-semibold text-gray-700 mb-6">识别结果</h2>

            <div id="resultArea" class="hidden">
                <div class="flex justify-between items-center mb-4">
                    <h3 class="text-lg font-medium text-gray-700">音频文本信息</h3>
                    <button id="copyBtn" class="bg-gray-200 hover:bg-gray-300 text-gray-700 text-sm font-medium py-1 px-3 rounded-md transition-colors duration-300 flex items-center">
                        <i class="fas fa-copy mr-1"></i>
                        复制JSON
                    </button>
                </div>

                <div class="json-container text-green-400">
                    <code id="jsonResult"></code>
                </div>

                <div id="responseInfo" class="mt-4 text-sm text-gray-600">
                    <p>响应时间: <span id="responseTime">-</span></p>
                </div>
            </div>

            <div id="emptyState" class="text-center py-12">
                <i class="fas fa-headphones text-4xl text-gray-300 mb-4"></i>
                <p class="text-gray-500">上传音频文件后，识别结果将显示在这里</p>
            </div>
        </section>
    </main>

    <footer class="text-center mt-12 text-gray-500 text-sm">
        <p>音频上传与识别系统 © 2025</p>
    </footer>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const audioFileInput = document.getElementById('audioFile');
        const uploadBtn = document.getElementById('uploadBtn');
        const fileInfo = document.getElementById('fileInfo');
        const fileName = document.getElementById('fileName');
        const fileType = document.getElementById('fileType');
        const fileSize = document.getElementById('fileSize');
        const resultArea = document.getElementById('resultArea');
        const emptyState = document.getElementById('emptyState');
        const jsonResult = document.getElementById('jsonResult');
        const copyBtn = document.getElementById('copyBtn');
        const responseTime = document.getElementById('responseTime');
        const uploadStatus = document.getElementById('uploadStatus');
        const statusMessage = document.getElementById('statusMessage');

        // 文件选择事件
        audioFileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 显示文件信息
                fileName.textContent = file.name;
                fileType.textContent = file.type || '未知类型';
                fileSize.textContent = formatFileSize(file.size);
                fileInfo.classList.remove('hidden');

                // 重置状态显示
                uploadStatus.classList.add('hidden');
                resultArea.classList.add('hidden');
                emptyState.classList.remove('hidden');
            } else {
                fileInfo.classList.add('hidden');
            }
        });

        // 上传按钮点击事件
        uploadBtn.addEventListener('click', function() {
            const file = audioFileInput.files[0];
            if (!file) {
                showStatus('请先选择音频文件', 'error');
                return;
            }

            // 验证文件类型
            const validTypes = ['audio/mp3', 'audio/webm', 'audio/wav', 'audio/mpeg', 'audio/x-wav'];
            if (!validTypes.includes(file.type)) {
                showStatus('请选择MP3、WebM或WAV格式的音频文件', 'error');
                return;
            }

            // 禁用上传按钮，防止重复点击
            uploadBtn.disabled = true;
            uploadBtn.innerHTML = '<i class="fas fa-spinner loading-spinner mr-2"></i>上传中...';

            // 调用后端接口
            callAudioUploadAPI(file);
        });

        // 调用后端接口
        function callAudioUploadAPI(file) {
            const formData = new FormData();
            formData.append('files', file);

            const startTime = new Date().getTime();

            // 使用fetch调用后端接口
            fetch('/audioUpload', {
                method: 'POST',
                body: formData
            })
                .then(response => {
                    const endTime = new Date().getTime();
                    responseTime.textContent = (endTime - startTime) + 'ms';

                    if (!response.ok) {
                        throw new Error('网络响应不正常，状态码: ' + response.status);
                    }
                    return response.json();
                })
                .then(data => {
                    // 显示结果区域
                    emptyState.classList.add('hidden');
                    resultArea.classList.remove('hidden');

                    // 格式化并显示JSON结果
                    jsonResult.textContent = JSON.stringify(data, null, 2);

                    showStatus('音频上传并识别成功！', 'success');
                })
                .catch(error => {
                    console.error('上传失败:', error);
                    showStatus('上传失败: ' + error.message, 'error');
                })
                .finally(() => {
                    // 重新启用上传按钮
                    uploadBtn.disabled = false;
                    uploadBtn.innerHTML = '<i class="fas fa-upload mr-2"></i>上传并识别';
                });
        }

        // 显示状态信息
        function showStatus(message, type) {
            statusMessage.textContent = message;
            uploadStatus.classList.remove('hidden');

            if (type === 'success') {
                statusMessage.className = 'text-center py-2 px-4 rounded-md upload-success';
            } else if (type === 'error') {
                statusMessage.className = 'text-center py-2 px-4 rounded-md upload-error';
            }
        }

        // 复制JSON按钮
        copyBtn.addEventListener('click', function() {
            const jsonText = jsonResult.textContent;
            navigator.clipboard.writeText(jsonText).then(() => {
                // 显示复制成功提示
                const originalText = copyBtn.innerHTML;
                copyBtn.innerHTML = '<i class="fas fa-check mr-1"></i>已复制';

                setTimeout(() => {
                    copyBtn.innerHTML = originalText;
                }, 2000);
            });
        })

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';

            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));

            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
    });
</script>
</body>
</html>